import Vue from 'vue'
// 导入路由
import VueRouter from 'vue-router'
// 引入Home组件
import Home from '@views/Home'
// 引入是否需要登录，获取localStorage中的token
import { isLogin } from '@utils'
// 使用路由
Vue.use(VueRouter)
// 创建一个路由组件对象
const routes = [
  {
    path: '/home',
    name: '首页',
    // 注册home组件
    component: Home,
    // 路由元信息
    meta: {
      needLogin: false,
      // 是否需要缓存的组件
      keepAlive: true
    }
  },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/cate',
    name: '分类',
    meta: {
      needLogin: false
    },
    component: () => import('@views/Cate')
  },
  {
    path: '/cart',
    name: '购物车',
    component: () => import('@views/Cart'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/user',
    name: '个人中心',
    component: () => import('@views/User'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/itemList/:cateId',
    name: '商品列表',
    component: () => import('@views/ItemList'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/detail',
    name: '商品详情',
    component: () => import('@views/Detail'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/login',
    name: '登录',
    component: () => import('@views/Login'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/order',
    name: '订单',
    component: () => import('@views/Order'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/phone',
    name: '咨询',
    // 按需引入组件
    component: () => import('@views/Phone'),
    // 路由元信息，判断是否需要登录
    meta: {
      needLogin: false
    }
  },
  {
    path: '/contactLists',
    name: '所有联系人',
    component: () => import('@views/ContactLists'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/contactEdit',
    name: '编辑联系人',
    component: () => import('@views/ContactEdit'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/contactAdd',
    name: '增加联系人',
    component: () => import('@views/ContactAdd'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/search',
    name: '搜索',
    component: () => import('@views/Search'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/searchResult',
    name: '搜素结果',
    component: () => import('@views/SearchResult'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/coupon',
    name: '优惠劵',
    component: () => import('@views/Coupon'),
    meta: {
      needLogin: true
    }
  }
]
// 创建一个路由对象
const router = new VueRouter({
  // history路由模式
  mode: 'history',
  // 实例化路由对象
  routes,
  // 滚动行为
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition
    } else {
      return {
        // 返回滚动的想x，y坐标
        x: 0,
        y: 0
      }
    }
  }
})
// 路由前置守卫
router.beforeEach((to, from, next) => {
  // 判断当前路由是否需要登录
  if (to.meta.needLogin) {
    // 需要登录
    if (isLogin()) {
      // 登录了，放开路由
      next()
    } else {
      // 跳转回登录页面
      next({
        path: '/login',
        // query传参，传入之前页面的path
        query: {
          from: to.path
        }
      })
    }
  } else {
    // 不需要登录
    next()
  }
})
// 导出路由
export default router
